package servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {

    private int counter;

    // 由于Servlet是单例多线程的，而其中又存在可修改的成员变量username，
    // 所以，当前这个LoginServlet存在线程安全问题，即这个Servlet是线程不安全的
    private String name;  //成员变量
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      /*  // 对成员变量的修改
       String  name = req.getParameter("username");

       synchronized (this) {

           name = req.getParameter("username");
           PrintWriter writer = resp.getWriter();
           writer.print("username = " + name);

       }*/


        counter++;

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("当前页面已经被浏览过"+counter+"次");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
